(function($) {
	"use strict";
	var uiBase = {
		doMain:function(){
			return "${pageContext.request.contextPath}${fns:getApiPath()}";
		},
		//创建一个GUID
		createGuid: function() {
			var guid = "";
			for(var i = 1; i <= 32; i++) {
				var n = Math.floor(Math.random() * 16.0).toString(16);
				guid += n;
				if((i == 8) || (i == 12) || (i == 16) || (i == 20)) guid += "-";
			}
			return guid;
		},
		//判断是否为空
		isNullOrEmpty: function(obj) {
			if((typeof(obj) == "string" && obj == "") || obj == null || obj == undefined) {
				return true;
			} else {
				return false;
			}
		},
		//判断是否为数字
		isNumber: function(obj) {
			$("#" + obj).bind("contextmenu", function() {
				return false;
			});
			$("#" + obj).css('ime-mode', 'disabled');
			$("#" + obj).keypress(function(e) {
				if(e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {
					return false;
				}
			});
		},
		//判断是否是金钱
		isMoney: function(obj) {
			$("#" + obj).bind("contextmenu", function() {
				return false;
			});
			$("#" + obj).css('ime-mode', 'disabled');
			$("#" + obj).bind("keydown", function(e) {
				var key = window.event ? e.keyCode : e.which;
				if(isFullStop(key)) {
					return $(this).val().indexOf('.') < 0;
				}
				return(isSpecialKey(key)) || ((isNumber(key) && !e.shiftKey));
			});

			function isNumber(key) {
				return key >= 48 && key <= 57
			}

			function isSpecialKey(key) {
				return key == 8 || key == 46 || (key >= 37 && key <= 40) || key == 35 || key == 36 || key == 9 || key == 13
			}

			function isFullStop(key) {
				return key == 190 || key == 110;
			}
		},
		//日期格式化yyyy-
		formatDate: function(v, format) {
			if(!v) return "";
			var d = v;
			if(typeof v === 'string') {
				if(v.indexOf("/Date(") > -1)
					d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
				else
					d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0])); //.split(".")[0] 用来处理出现毫秒的情况，截取掉.xxx，否则会出错
			}
			var o = {
				"M+": d.getMonth() + 1, //month
				"d+": d.getDate(), //day
				"h+": d.getHours(), //hour
				"m+": d.getMinutes(), //minute
				"s+": d.getSeconds(), //second
				"q+": Math.floor((d.getMonth() + 3) / 3), //quarter
				"S": d.getMilliseconds() //millisecond
			};
			if(/(y+)/.test(format)) {
				format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
			}
			for(var k in o) {
				if(new RegExp("(" + k + ")").test(format)) {
					format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
				}
			}
			return format;
		},
		//转化成十进制
		toDecimal: function(num) {
			if(num == null) {
				num = "0";
			}
			num = num.toString().replace(/\$|\,/g, '');
			if(isNaN(num))
				num = "0";
			sign = (num == (num = Math.abs(num)));
			num = Math.floor(num * 100 + 0.50000000001);
			cents = num % 100;
			num = Math.floor(num / 100).toString();
			if(cents < 10)
				cents = "0" + cents;
			for(var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
				num = num.substring(0, num.length - (4 * i + 3)) + '' +
				num.substring(num.length - (4 * i + 3));
			return(((sign) ? '' : '-') + num + '.' + cents);
		},
		//数组复制
		arrayCopy: function(data) {
			return $.map(data, function(obj) {
				return $.extend(true, {}, obj);
			});
		},
		info: function (msg) {
            layer.msg(msg);
        },

        warning: function (msg) {//通知
            layer.msg(msg, {icon: 0});
        },

        success: function (msg) {
            layer.msg(msg, {icon: 1});
        },

        error: function (msg) {
            layer.msg(msg, {icon: 2});
        },
		get: function (url, callback) {
			$.ajax({
				url: url,
				method: "get",
				dataType:'json',
				error: function (xhr, textStatus) {
					if (xhr.status == 0) {
						layer.msg("连接失败，请检查网络!")
					} else if (xhr.status == 404) {
						var errDetail = "<font color='red'>404,请求地址不存在！</font>";
						layer.msg(errDetail, {
							icon: 2,
							area: ['auto', 'auto'],
							title: "请求出错"
						})
					} else if (xhr.status && xhr.responseText) {
						var errDetail = "<font color='red'>" + xhr.responseText.replace(/[\r\n]/g, "<br>").replace(/[\r]/g, "<br>").replace(/[\n]/g, "<br>") + "</font>";
						layer.msg(errDetail, {
							icon: 2,
							area: ['80%', '70%'],
							title: xhr.status + "错误"
						})
					} else {
						var errDetail = "<font color='red'>未知错误!</font>";
						layer.msg(errDetail, {
							icon: 2,
							area: ['auto', 'auto'],
							title: "真悲剧，后台抛出异常了"
						})
					}

				},
				success: function (data, textStatus, jqXHR) {
					alert(JSON.stringify(data))
					if (data.indexOf == "_login_page_") {//返回首页内容代表登录超时
						top.layer.alert("登录超时！")
						location.reload(true);
					} else {
						callback(data);
					}
					//callback(data);
				}
			});
		}
	}
	$.uiBase = uiBase;
})(jQuery)
$.fn.extend(String.prototype, {
	isNullOrEmpty: function() {
		return $.uiBase.isNullOrEmpty(this.toString());
	},
	formart: function() {
		if(arguments.length == 0)
			return null;
		var str = arguments[0];
		for(var i = 1; i < arguments.length; i++) {
			var re = new RegExp('\\{' + (i - 1) + '\\}', 'gm');
			str = str.replace(re, arguments[i]);
		}
		return str;
	}
})
$.fn.extend({
	str: function() {
		alert('ab')
	}
})
$.fn.extend(Number.prototype, {

})
$(function(){
	var layer;
layui.use(['layer', 'form'], function() {
	layer = layui.layer,
		form = layui.form;
});
})
